CREATE TABLE [dbo].[tb_MatriculasDivision]
(
[Division] [int] NOT NULL,
[MatriculaMujeres] [int] NULL,
[MatriculaVarones] [int] NULL,
[Matricula] [int] NOT NULL,
[Aula] [int] NULL,
[Repitentes] [int] NULL,
[Repitentes2veces] [int] NULL,
[FechaAlta] [datetime] NOT NULL,
[Baja] [varchar] (1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[FechaBaja] [datetime] NULL,
[idMatricula] [int] NOT NULL IDENTITY(1, 1)
) ON [PRIMARY]
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
CREATE TRIGGER [dbo].[BajasLogicastb_MatriculasDivision] ON [dbo].[tb_MatriculasDivision]
FOR UPDATE AS
SET NOCOUNT ON
if update(baja) and (Select baja from inserted)='S' and (Select FechaBaja from inserted) is not null BEGIN
	UPDATE tb_MatriculasDivision set FechaBaja = getdate() where idMatricula=(Select idMatricula from inserted)
END
else
if (update(baja) and (Select baja from inserted)='N')	UPDATE tb_MatriculasDivision set FechaBaja= null where idMatricula=(Select idMatricula from inserted)
GO
ALTER TABLE [dbo].[tb_MatriculasDivision] ADD CONSTRAINT [PK_tb_MatriculasDivision] PRIMARY KEY NONCLUSTERED  ([idMatricula]) ON [PRIMARY]
GO
ALTER TABLE [dbo].[tb_MatriculasDivision] WITH NOCHECK ADD CONSTRAINT [FK_tb_MatriculasDivision_tb_Divisiones] FOREIGN KEY ([Division]) REFERENCES [dbo].[tb_Divisiones] ([idDivision])
GO
ALTER TABLE [dbo].[tb_MatriculasDivision] NOCHECK CONSTRAINT [FK_tb_MatriculasDivision_tb_Divisiones]
GO
EXEC sp_bindrule N'[dbo].[S-N]', N'[dbo].[tb_MatriculasDivision].[Baja]'
GO
